package com.woniuxy.util;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.EncodeException;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

public class WebSocketUtil {
	
	public static final Map<String, Session> MESSAGEMAP = new ConcurrentHashMap<String, Session>();
	
	public static final Map<String, String> USERMAP = new ConcurrentHashMap<String, String>();
	
	public static final Map<String, String> ORDERMAP = new ConcurrentHashMap<String, String>();
	//发送消息给客户端
	public static void sendMessage(Session session,Object order){
		if(session!=null){
			final RemoteEndpoint.Basic basic = session.getBasicRemote();
			if(basic!=null){
				try {
					basic.sendObject(order);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (EncodeException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//将消息发送给固定某个人
	public static void sendMessageToOne(String userName, Object order){
		Object object = MESSAGEMAP.get(userName);
		if(object!=null){
			Session session = (Session) object;
			sendMessage(session, order);
		}
	}

}